/**
 * William Holland & Michael Vega
 * Operating Systems - Spring 2011
 * 
 * BookKeeper.java
 * 
 * A Class designed to keep track of various 
 * accounting information as required in the 
 * project specifications document.
 */

import java.util.Vector;
import java.util.concurrent.Semaphore;

public class BookKeeper {
	
	private static Vector<Long> waitTimes = new Vector<Long>();
	private static Vector<Long> execTimes = new Vector<Long>();
	private static Vector<Integer> IORequest = new Vector<Integer>();
	private static Vector<Integer> instructions = new Vector<Integer>();
	private static float avgWait, avgExec, avgIO, avgInst;
	public static int faults = 0;
	public static Semaphore s = new Semaphore(1, true);
	public static String[] results;
	public static void addWait(long time){
		waitTimes.add(time);
	}
	
	public static void addExec(long time){
		execTimes.add(time);
	}
	
	public static void addIO(int i){
		IORequest.add(i);
	}
	
	public static void addInstructions(int i){
		instructions.add(i);
	}
	public static void addResult(int position, String string){
		try {
			s.acquire();
			results[position] = string;
			s.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
	private static void calculateStats(){
		//All of the vectors should have the same number of elements
		long tempW = 0;
		long tempE = 0;
		int tempIO = 0;
		int tempIN = 0;
		for(int i = 0; i < waitTimes.size(); i++){
			tempW += waitTimes.elementAt(i);
			tempE += execTimes.elementAt(i);
			tempIO += IORequest.elementAt(i);
			tempIN += instructions.elementAt(i);
		}
		
		avgWait = (float)(tempW / waitTimes.size());
		avgExec = (float)(tempE / waitTimes.size());
		avgIO = (float)(tempIO / waitTimes.size());
		avgInst = (float)(tempIN / waitTimes.size());
	}
	
	public static void reportResults(){
		calculateStats();
		for(String s: results)
			System.out.println(s);
		System.out.println("Number of Processes: " + waitTimes.size());
		System.out.println("Number of Faults   : " + faults);
		System.out.println("Average Wait Time  : " + avgWait);
		System.out.println("Average Exec Time  : " + avgExec);
		System.out.println("Average IO Requests: " + avgIO);
		System.out.println("Average # of Instructions: " + avgInst);
		
	}

}